/**
 * 
 */
package com.vchello.util;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

/**
 * 工具类
 * @author power
 * @time 2015年11月12日下午4:16:56
 */
public class ToolUtil {
    
    public static final String DATE_TIME_FOMAT = "yyyy-MM-dd HH:mm:ss";

    public static String getFixStr(String cn) {
	if (null == cn) {
	    return "";
	}
	String str = cn;
	str = str.replaceAll("[']", "");
	str = str.replaceAll("[\"]", "");
	str = str.replaceAll("[\\\\]", "");
	str = str.replaceAll("[/]", "");
	str = str.replaceAll("[~]", "");
	str = str.replaceAll("[!]", "");
	str = str.replaceAll("[@]", "");
	str = str.replaceAll("[#]", "");
	str = str.replaceAll("[$]", "");
	str = str.replaceAll("[%]", "");
	str = str.replaceAll("[\\^]", "");
	str = str.replaceAll("[&]", "");
	str = str.replaceAll("[*]", "");
	str = str.replaceAll("[(]", "");
	str = str.replaceAll("[)]", "");
	str = str.replaceAll("[<]", "");
	str = str.replaceAll("[>]", "");
	str = str.replaceAll("[?]", "");
	str = str.replaceAll("[,]", "");
	str = str.replaceAll("[+]", "");
	str = str.replaceAll("[-]", "");
	str = str.replaceAll("[（]", "");
	str = str.replaceAll("[）]", "");
	str = str.replaceAll("[\\[]", "");
	str = str.replaceAll("[\\]]", "");
	str = str.replaceAll("[\\{]", "");
	str = str.replaceAll("[\\}]", "");
	str = str.replaceAll(" ", "");
	str = str.replaceAll("'", "");
	str = str.replaceAll("or", "");

	return str;
    }

    /**
     * 分割字符串为数组
     * 
     * @param s
     *            "aa|||||c|||d"
     * @param regex
     *            "|"
     * @return {"aa","c","d"}
     */
    public static String[] split(String s, String regex) {
	if (s == null) {
	    return new String[] {};
	}
	String[] temp = s.split(regex);
	List<String> list = clearEmpty(temp);

	String[] result = new String[list.size()];
	for (int i = 0; i < list.size(); i++) {
	    result[i] = list.get(i);
	}

	return result;
    }

    /**
     * 数组拼接为字符串
     * 
     * @param strs
     *            {"a", "b", "c"}
     * @param regex
     *            ", "
     * @return "a, b, c"
     */
    public static String parseToString(String[] strs, String regex) {
	String result = "";
	if (strs == null) {
	    return result;
	}
	for (int i = 0; i < strs.length; i++) {
	    if ("".equals(result))
		result = strs[i];
	    else
		result = result.concat(regex + strs[i]);
	}

	return result;
    }

    /**
     * 分割字符串为List
     * 
     * @param s
     *            "aa|||||c|||d|aa|c"
     * @param regex
     *            "|"
     * @return aa,c,d,aa,c
     */
    public static List<String> splitToList(String s, String regex) {
	if (s == null) {
	    return new ArrayList<String>();
	}
	String[] temp = s.split(regex);
	List<String> list = clearEmpty(temp);

	return list;
    }

    /**
     * 可以去重复 分割字符串为Set()
     * 
     * @param s
     *            "aa|||||c|||d|aa|c"
     * @param regex
     *            "|"
     * @return aa,c,d
     */
    public static Set<String> splitToSet(String s, String regex) {
	Set<String> set = new HashSet<String>();
	if (s == null) {
	    return set;
	}
	String[] temp = s.split(regex);
	for (int i = 0; i < temp.length; i++) {
	    set.add(temp[i]);
	}

	return set;
    }

    /**
     * 去除空串
     * 
     * @param strArray
     *            {"a", "", "b", "c","  ", "d"}
     * @return ["a", "b", "c", "d"]
     */
    public static List<String> clearEmpty(String[] strArray) {
	List<String> list = new ArrayList<String>();

	for (int i = 0; i < strArray.length; i++) {
	    if (strArray[i] == null || "".equals(strArray[i].trim())) {
		continue;
	    }
	    list.add(strArray[i].trim());
	}
	return list;
    }

    public static long getLong(String str, long defaultValue) {

	if (str == null) {
	    return defaultValue;
	}

	long res = Long.MIN_VALUE;
	try {
	    res = Long.valueOf(str.trim());
	} catch (Exception e) {
	    res = defaultValue;
	}
	return res;
    }

    public static int getInt(String str, int defaultValue) {

	if (str == null) {
	    return defaultValue;
	}

	int res = Integer.MIN_VALUE;
	try {
	    res = Integer.valueOf(str.trim());
	} catch (Exception e) {
	    res = defaultValue;
	}
	return res;
    }

    public static int getInt(String str) {
	try {
	    return Integer.valueOf(str.trim());
	} catch (Exception e) {
	}
	return Integer.MIN_VALUE;
    }

    public static byte[] getBytes(String args) {
	byte[] res = new byte[] {};

	args = replaceBlank(args);

	try {
	    res = (args == null ? res : args.getBytes("UTF-8"));
	} catch (UnsupportedEncodingException e) {
	    e.printStackTrace();
	}

	return res;
    }

    public static byte[] getBytes(String args, String encoding) {
	byte[] res = new byte[] {};

	try {
	    res = (args == null ? res : args.getBytes(encoding));
	} catch (UnsupportedEncodingException e) {
	    e.printStackTrace();
	}

	return res;
    }

    private static String replaceBlank(String args) {
	if (args == null) {
	    return null;
	}

	Pattern pat = Pattern.compile("\r\n");
	Matcher m = pat.matcher(args);
	args = m.replaceAll("\n");

	// args = args.replaceAll("\r\n", "\n");
	return args;
    }

    public static Date getDate(String dateStr, String formatStr) {
	if (isEmpty(formatStr)) {
	    formatStr = DATE_TIME_FOMAT;
	}

	SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
	try {
	    return sdf.parse(dateStr);
	} catch (Exception e) {
	    return null;
	}
    }

    public static boolean isEmpty(String str) {

	if (str == null || "".equals(str.trim())) {
	    return true;
	}

	return false;
    }

    public static boolean isNotEmpty(String str) {

	if (str != null && !"".equals(str.trim())) {
	    return true;
	}

	return false;
    }

    public static boolean isArrayListEmpty(List<?> list) {
	if(list == null || list.size() < 0) {
	    return true;
	}
	return false;

    }
    
    public static boolean isArrayListNotEmpty(List<?> list) {
	if(list != null && list.size() > 0) {
	    return true;
	}
	return false;
	
    }

    /**
     * 得到十六位的唯一id
     * 
     * @return
     */
    public static String generateWord() {
	String[] beforeShuffle = new String[] { "0", "1", "2", "3", "4", "5",
		"6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h",
		"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
		"u", "v", "w", "x", "y", "z" };
	List<String> list = Arrays.asList(beforeShuffle);
	Collections.shuffle(list);
	StringBuilder sb = new StringBuilder();
	for (int i = 0; i < list.size(); i++) {
	    sb.append(list.get(i));
	}
	String afterShuffle = sb.toString();
	String result = afterShuffle.substring(5, 21);
	return result;
    }
    
    public static boolean isweixin(HttpServletRequest request){
		String ua = ((HttpServletRequest) request).getHeader("user-agent")
		          .toLowerCase();
		if (ua.indexOf("micromessenger") > 0) {// 是微信浏览器
			return true;
		}
		return false;
	}
}
